package com.phkj.qdh.ui.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.phkj.qdh.base.BaseDialogFragment
import com.phkj.qdh.databinding.DialogCommonCancelBinding

class CommonConfirmDialog : BaseDialogFragment() {
    private var _binding: DialogCommonCancelBinding? = null
    private val binding get() = _binding!!
    private var title: String? = null
    private var content: String? = null
    private var btnOkText: String? = null
    private var btnCancelText: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            title = it.getString("title")
            content = it.getString("content")
            btnOkText = it.getString("btnOkText")
            btnCancelText = it.getString("btnCancelText")
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = DialogCommonCancelBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.run {
            tvTitle.text = title
            tvContent.text = content
            btnOk.text = btnOkText?:"确定"
            btnNext.text = btnCancelText?:"取消"
            btnNext.setOnClickListener {
                listener?.dCancel()
                this@CommonConfirmDialog.dismiss()
            }
            btnOk.setOnClickListener {
                listener?.dOk()
                this@CommonConfirmDialog.dismiss()
            }
        }
    }

    private var listener: CommonConfirmListener? = null
    fun setCancelListener(l: CommonConfirmListener) {
        this.listener = l
    }


    companion object {
        @JvmStatic
        fun getInstance(title: String, content: String,btnOkText:String = "确定",btnCancelText:String = "取消") = CommonConfirmDialog().apply {
            this.arguments = Bundle().apply {
                this.putString("title", title)
                this.putString("content", content)
                this.putString("btnOkText", btnOkText)
                this.putString("btnCancelText", btnCancelText)
            }
        }
    }
}

interface CommonConfirmListener{
    fun dOk()

    fun dCancel()
}